
import csv
# 写一个csv文件当做电话簿
# with open("denwa.csv","w",encoding="utf-8") as f:
#     writer1 = csv.writer(f)
#     writer1.writerow(["姓名", "电话号码", "地址"])
#     writer1.writerows([["赵三",18434795720,"临汾"],
#                        ["钱三",18434795721,"运城"],
#                        ["孙三",18434795722,"贵州"],
#                        ["李三",18434795723,"成都"],
#                        ["周三",18434795724,"重庆"],
#                        ["吴三",18434795725,"大同"],
#                        ["郑三",18434795726,"郑州"],
#                        ["王三",18434795727,"西安"]])
# 查看所有信息
def listphones():
    with open("denwa.csv", "r", encoding="utf-8") as f:
        reader2 = csv.DictReader(f)
        for row in reader2:  # row是一行数据，类型为字典
            print( row.get("姓名"), '：', row["电话号码"],"\t",row["地址"])

# 查看单个电话信息
def showphone(name):
    with open("denwa.csv", "r", encoding="utf-8") as f:
        reader3 = csv.DictReader(f)
        for row in reader3:  # row是一行数据，类型为字典
            if row.get("姓名")==name:
                print(row.get("姓名"), '：', row["电话号码"],"\t",row["地址"])


# 根据姓名删除信息
def delete(name):
    # 空列表
    rows = []
    # 边读边往列表里面添加
    with open("denwa.csv", "r", encoding="utf-8") as f:
        reader4 = csv.DictReader(f)
        fieldnames1 = reader4.fieldnames
        for row in reader4:
            if row["姓名"] != name:
                rows.append(row)
    # 如果是删除的name 就不添加
    with open("denwa.csv", "w", encoding="utf-8") as f:
        writer2 = csv.DictWriter(f, fieldnames=fieldnames1)  # 写入列标题
        writer2.writerows(rows)
        print(f"已删除姓名为 '{name}' 的联系人")
        # 写  将列表中的数据覆盖到文件中


# 修改信息根据姓名修改信息
def change(name, newname, newphone, newaddress):
    rows1 = []
    # 读取所有数据
    with open("denwa.csv", "r", encoding="utf-8") as f:
        reader5 = csv.DictReader(f)
        fieldnames2 = reader5.fieldnames
        for row in reader5:
            if row["姓名"] == name:
                # 更新字段
                # newname = input("姓名:")
                # newphone = input("电话号码:")
                # newaddress = input("地址:")
                rows1.append(newname,newphone,newaddress)

    # 写回文件
    with open("denwa.csv", "w", encoding="utf-8", newline='') as f:
        writer = csv.DictWriter(f, fieldnames=fieldnames2)
        writer.writerows(rows1)

# 添加
def add(name,phone,address):
    with open("denwa.csv", "a", encoding="utf-8") as f:
        w = csv.DictWriter(f, fieldnames=["姓名", "电话号码","地址"])
        w.writerow({"姓名": name, "电话号码": phone,"地址":address})

def main():
    print("------欢迎进入电话簿系统--------")

    while True:

        print("------1 查看所有电话信息--------")
        print("------2 查看单个电话信息--------")
        print("------3 根据姓名删除信息--------")
        print("------4 根据姓名修改信息--------")
        print("------5 添加电话信息   --------")
        print("------6 退出系统      --------")
        num = input("------请输入你的操作--------\n")
        if num == '1':
            listphones()
        elif num == '2':
            name  = input("请输入需要查看的姓名：")
            showphone(name)
        elif num == '3':
            name = input("请输入需要删除的姓名：")
            delete(name)

        elif num == '4':
            name = input("请输入需要修改的姓名：")
            newname = input("姓名:")
            newphone = input("电话号码:")
            newaddress = input("地址:")
            change(name,newname,newphone,newaddress)
        elif num == '5':
            # 添加电话信息
            name = input("请输入需要添加的姓名：")
            phone = input("电话号码：")
            address = input("地址：")
            add(name,phone,address)
        else:
            # 退出循环
            break
main()